package com.kelvin.paopao.module.login.login.bindAccount;

import android.content.Intent;

import com.hyphenate.EMCallBack;
import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.UserDetailModel;
import com.kelvin.paopao.easemob.EaseMobHelper;
import com.kelvin.paopao.module.base.IBasePresenter;
import com.kelvin.paopao.module.main.MainActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.DialogMaker;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/7/14.
 */

public class BindAccountPresenter implements IBindAccountPresenter {
    private BindAccountActivity mView;

    public BindAccountPresenter(BindAccountActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }

    @Override
    public void bindAccount(String userId, String password, String wechat_id) {
        RetrofitService.login(userId, password, wechat_id).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView, "登录中,请稍候~~");
            }
        }).subscribe
                (new Consumer<CommonListResponse<UserDetailModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<UserDetailModel>
                                               userDetailModelCommonListResponse) throws Exception {
                        if (userDetailModelCommonListResponse.getReason().equals("成功")) {
                            String user_no = userDetailModelCommonListResponse.getResult().get(0).getUser_no();
                            SpUtil.setLoginHuanxin_id(mView, user_no);

                            String user_nickname = userDetailModelCommonListResponse.getResult().get(0).getUser_name();
                            String user_headimg = userDetailModelCommonListResponse.getResult().get(0).getUser_headimg();

                            SpUtil.setLoginUserName(mView, user_nickname);
                            SpUtil.setLoginAvatar(mView, user_headimg);

                            String user_id = userDetailModelCommonListResponse.getResult().get(0).getUser_tel();
                            SpUtil.setLoginphone(mView, user_id);

                            EaseMobHelper.getInstance().login(user_no, new EMCallBack() {
                                @Override
                                public void onSuccess() {
                                    Intent intent = new Intent(mView, MainActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    mView.startActivity(intent);
                                    DialogMaker.diamiss();
                                    Constant.IS_LOGIN = true;
                                }

                                @Override
                                public void onError(int i, String s) {
                                    ToastUtil.showToast(mView, "登录失败");
                                    DialogMaker.diamiss();
                                }

                                @Override
                                public void onProgress(int i, String s) {

                                }
                            });
                        } else {
                            ToastUtil.showToast(mView, userDetailModelCommonListResponse.getReason());
                            DialogMaker.diamiss();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {

                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {

                    }
                });
    }
}
